package com.group.Controller;

import cn.hutool.json.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.group.Enity.Building;
import com.group.Enity.CarCharge;
import com.group.Service.BuildingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("building")
public class BuildingController {

    @Autowired
    private BuildingService buildingService;

    @ResponseBody
    @RequestMapping("queryBuildAll.do")
    public JSONObject queryBuildAll(Integer page,Integer limit)
    {

        PageHelper.startPage(page,limit);
        List<Building> list = buildingService.getBuilds(null);
        PageInfo pageInfo = new PageInfo<>(list);
        Long len = pageInfo.getTotal();
        List<CarCharge> list_new = pageInfo.getList();

        JSONObject json=new JSONObject();
        json.accumulate("count",len);
        json.accumulate("code",0);
        json.accumulate("msg","null");
        json.accumulate("data",list_new);
        return json;
    }

    @ResponseBody
    @RequestMapping("searchBuildings.do")
    public JSONObject searchBuildings(Integer page,Integer limit,String numbers)
    {

        PageHelper.startPage(page,limit);
        List<Building> list = buildingService.getBuilds(numbers);
        PageInfo pageInfo = new PageInfo<>(list);
        Long len = pageInfo.getTotal();
        List<CarCharge> list_new = pageInfo.getList();

        JSONObject json=new JSONObject();
        json.accumulate("count",len);
        json.accumulate("code",0);
        json.accumulate("msg","null");
        json.accumulate("data",list_new);
        return json;
    }

    @ResponseBody
    @RequestMapping("addBuild.do")
    public JSONObject addBuild(String numbers,String unit,String remarks){
        //System.out.println(numbers);
        //System.out.println(unit);
        Building building = new Building();
        building.setNumbers(numbers);
        building.setUnit(unit);
        building.setRemarks(remarks);

        buildingService.insertBuild(building);
        JSONObject json=new JSONObject();
        json.accumulate("code",0);
        return json;
    }


    @ResponseBody
    @RequestMapping("queryBuild")
    public List<Building> queryBuild()
    {
        List<Building> list = buildingService.getBuilds(null);
        return list;
    }


    @ResponseBody
    @RequestMapping("updateBuild.do")
    public String updateBuild(int id,String unit,String remarks){

        System.out.println("id:" + id + ",unit:" + unit + ",marks:" + remarks);
        buildingService.edit(id,unit,remarks);
        return "{\"code\":200}";
    }

    @ResponseBody
    @RequestMapping("deleteByIds.do")
    public String deleteByIds(String ids){
        //System.out.println(ids);
        String[] lst = ids.split(",");
        for (int index = 0;index < lst.length;index++)
        {
            int id = Integer.valueOf(lst[index]);
            buildingService.delete(id);

        }

        JSONObject json=new JSONObject();
        json.accumulate("code",200);
        return json.toString();
    }




}
